﻿using MediatR;
using SmartTracking.API.Write.Application.Commands.Permission_command;
using SmartTracking.Domain.UserInfo;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;

namespace SmartTracking.API.Write.Application.CommandHandlers.Permission_handler
{
    public class DeletePermissionCommandHandler : IRequestHandler<DeletePermissionCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Permissions> permissionRep;

        public DeletePermissionCommandHandler(
            IBaseRepository<Permissions> permissionRep)
        {
            this.permissionRep = permissionRep;
        }

        public async Task<ApiResult<int>> Handle(DeletePermissionCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.删除成功;
            result.Message = ApiEnum.删除成功.ToString();

            var list = permissionRep.GetAll().FirstOrDefault(x => x.PermissionId == request.PermissionId);
            list.IsDeleted = true;
            var res = await permissionRep.UpdateAsync(list);

            result.data = res;
            return await Task.FromResult(result);

        }
    }
}